<?php
/**
 * 报表管理
 * @author jianhui
 *
 */
class Report extends MY_Controller{
    var $reportType;
    function Report(){
        parent::MY_Controller();
        $this->typeList = array(1=>'专营店双月反馈列表报表',2=>'专营店双月反馈明细列表报表');
        $this->load->model('timeUnitModel');
    }
    /**
     * 报表管理首页
     */
    function index(){
        //查询条件
        //专营店筛选
        $area = $this->input->get('area');
        $this->viewData['area'] = $area;
        $region = $this->input->get('region');
        $this->viewData['region'] = $region;
        $province = $this->input->get('province');
        $this->viewData['province'] = $province;
        $city = $this->input->get('city');
        $this->viewData['city'] = $city;
        $storeId = $this->input->get('storeId');
        $this->viewData['storeId'] = $storeId;
        $keyword = $this->input->get('keyword');
        $this->viewData['keyword'] = $keyword;
        //年份
        $year = $this->input->get('year');
        $year || $year = $this->config->item('nowYear');
        $this->viewData['year'] = $year;
        //时间单位
        $timeUnit = (int)$this->input->get('timeUnit');
        $timeUnit || $timeUnit = $this->timeUnitModel->getNowTimeUnit();
        $this->viewData['timeUnit'] = $timeUnit;
        //数据类型
        $type = (int)$this->input->get('type');
        if (isset($_GET['submit']) && $_GET['submit'] == 'export'){
            isset($this->typeList[$type]) || showError('请选择正确的报表类型');
            //条件参数
            $params = array('area'=>$area, 'region'=>$region, 'province'=>$province, 'city'=>$city, 'storeId'=>$storeId, 'keyword'=>$keyword, 'year'=>$year, 'timeUnit'=>$timeUnit);
            //导出文件名
            $fileName = $this->typeList[$type].'-'.$year.'年';
            if ($timeUnit){
                $timeUnitList = $this->timeUnitModel->getCacheData('timeUnitList');
                isset($timeUnitList[$year][$timeUnit]) && $fileName .= '-'.$timeUnitList[$year][$timeUnit];
            }
            //设置excel文件下载头
            setExcelDownHeader($fileName);
            switch ($type){
                case 1://专营店反馈报表
                    $this->getStoreProExcel($params);
                    break;
                case 2://专营店反馈明细报表
                    $this->getStoreProDetailExcel($params);
                    break;
            }
        }else{
            //面包屑
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('Report', 'index'),'title'=>'报表管理');
    
            $this->load->view('Report/index', $this->viewData);
        }
    }
    //专营店反馈报表
    function getStoreProExcel($params = array()){
        //专营店反馈条件
        $storeProWhere = array();
        //查找对应地区列表
        $placeStoreList = $this->sellpointModel->searchStore($params['area'], $params['region'], '', $params['province'], $params['city'], $params['storeId'], $params['keyword']);
        $placeStoreIds = array_keys($placeStoreList);
        $this->viewData['allStoreList'] = $placeStoreList;
        //权限内专营店列表
        $viewStoreIds = $this->userLib->getViewStoreIds();
        if ($viewStoreIds == '*'){
            $storeProWhere['storeId'] = array('in', $placeStoreIds);
        }else{
            $storeProWhere['storeId'] = array('in', array_intersect($viewStoreIds, $placeStoreIds));
        }
        $storeProWhere['year'] = $params['year'];
        $storeProWhere['timeUnit'] = $params['timeUnit'];
        //项目列表
        $this->load->model('projectModel');
        $proList = $this->projectModel->getNewList(array('isDel'=>0,'year'=>$params['year'],'timeUnit'=>$params['timeUnit']), '', '', '', 'proId desc');
        $newProList = array();
        foreach ($proList as $v){
            $newProList[(int)$v['proId']] = $v;
        }
        $this->viewData['proList'] = $newProList;
        //专营店反馈列表
        $this->load->model('storeProModel');
        $storeProWhere['proId'] = array('in', array_keys($newProList));
        $storeProList = $this->storeProModel->getNewList($storeProWhere, '', '', '', 'id desc');
        $this->viewData['storeProList'] = $storeProList;
        $this->load->view('Report/storeProExcel', $this->viewData);
    }
    /**
     * 导出专营店明细表
     * @param array $params
     */
    function getStoreProDetailExcel($params = array()){
        //专营店反馈条件
        $storeProWhere = array();
        //查找对应地区列表
        $placeStoreList = $this->sellpointModel->searchStore($params['area'], $params['region'], '', $params['province'], $params['city'], $params['storeId'], $params['keyword']);
        $placeStoreIds = array_keys($placeStoreList);
        $this->viewData['allStoreList'] = $placeStoreList;
        //权限内专营店列表
        $viewStoreIds = $this->userLib->getViewStoreIds();
        if ($viewStoreIds == '*'){
            $storeProWhere['storeId'] = array('in', $placeStoreIds);
        }else{
            $storeProWhere['storeId'] = array('in', array_intersect($viewStoreIds, $placeStoreIds));
        }
        $storeProWhere['year'] = $params['year'];
        $storeProWhere['timeUnit'] = $params['timeUnit'];
        //项目列表
        $this->load->model('projectModel');
        $proList = $this->projectModel->getNewList(array('isDel'=>0,'year'=>$params['year'],'timeUnit'=>$params['timeUnit']), '', '', '', 'proId desc');
        $this->load->model('proActModel');
        $newProList = array();
        foreach ($proList as $v){
            $itemCount = 0;
            $actList = $this->proActModel->getActListByProId($v['proId']);
            foreach ($actList as $actK => $actV) {
                $itemCount += count((array)$actV['itemList']);
            }
            $v['actList'] = $actList;
            $v['actCount'] = count((array)$actList);
            $v['itemCount'] = $itemCount;
            $newProList[(int)$v['proId']] = $v;
        }
        $this->viewData['proList'] = $newProList;
        //专营店反馈列表
        $this->load->model('storeProModel');
        $storeProWhere['proId'] = array('in', array_keys($newProList));
        $storeProList = $this->storeProModel->getNewList($storeProWhere, '', '', '', 'id desc');
        $storeProNewList = array();
        foreach ($storeProList as $v){
            isset($storeProNewList[(int)$v['storeId']]) || $storeProNewList[(int)$v['storeId']] = array();
            $storeProNewList[(int)$v['storeId']][(int)$v['proId']] = $v;
        }
        $this->viewData['storeProList'] = $storeProNewList;
        $this->load->view('Report/storeProDetailExcel', $this->viewData);
    }
}